﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.RobotCommunication.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.RobotCommunication
{
    public sealed class MsgHandler : IDisposable
    {
        private bool _isDisposed;

        private MsgSender _msgSender = null;

        public Action<ProcessMsg> ReceiveMsg;

        public MsgHandler(string ip, string port)
        {
            _msgSender = new MsgSender(ip, port);
        }

        public void Send(ProcessMsg msg)
        {
            _msgSender.Send(msg.comMsg.ToJson());
        }

        /// <summary>
        /// 释放
        /// </summary>
        /// <param name="disposing"></param>
        private void Dispose(bool disposing)
        {
            if (!this._isDisposed)
            {
                if (disposing)
                {
                    this.OnDisposing();
                }
                this._isDisposed = true;
            }
        }

        private void OnDisposing()
        {
            _msgSender.Dispose();
        }

        ~MsgHandler()
        {
            this.Dispose(false);
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
